package Generics_Reflect;

class MojaLista<T> {
	private class Node {
		T[] array;
		Node next;
		
		Node(T[] a, Node next) {this.array = a;this.next = next;}
		Node() {this(null, null);}
		boolean end() {return array == null && next == null;}
		public String toString(){
			String res="";
			for(int i=0; i<array.length-1;i++)res+=array[i]+", ";
			res+=array[array.length-1];
			return res;
		}
		T remove(int index) {
			T tmp = array[index];
			array[index] = null;
			return tmp;
		}
		
	}

	private Node top = new Node();
	public void add(T[] arrayToAdd) {top = new Node(arrayToAdd, top);}
	public int size() {
		Node tmp=top;
		int s=0;
		while(!tmp.end()){
			for(int i=0; i<tmp.array.length;i++){
				if(tmp.array[i]!=null)s++;
			}
			tmp=tmp.next;
		}
		return s;
	}

	public void clear() {top = new Node();}

	public T get(int index) {
		Node tmp=top;
		T res=null;
		while(!tmp.end()){
			for(int i=0; i<tmp.array.length;i++){
				if(tmp.array[i]!=null)index--;
				if(index==-1){
					res=tmp.array[i];
					break;
				}
			}
			tmp=tmp.next;
		}
		return res;
	}

	public T remove(int index) {
		Node tmp=top;
		T res=null;
		while(!tmp.end()){
			for(int i=0; i<tmp.array.length;i++){
				if(tmp.array[i]!=null)index--;
				if(index==-1){
					res=tmp.remove(i);
					break;
				}
			}
			tmp=tmp.next;
		}
		
		return res;
	}
	
	public String toString(){
		String res="";
		Node tmp=top;
		while(!tmp.end()){
			res+="["+tmp+"]";
			tmp=tmp.next;
		}
		return res;
	}

}

public class TestList {
	public static void main(String[] args) {
		MojaLista<Integer> ml = new MojaLista<Integer>();
		
		ml.add(new Integer[]{0});
		ml.add(new Integer[]{0,2,3,4});
		ml.add(new Integer[]{0,10,2,1});
		ml.add(new Integer[]{30,2,3});
		ml.add(new Integer[]{1,2,3});
		System.out.println(ml.size());
		System.out.println(ml);
		System.out.println("Usuwam: "+ml.remove(7));
		System.out.println(ml);
		System.out.println(ml.size());
		System.out.println("Usuwam: "+ml.remove(7));
		System.out.println(ml);
		System.out.println(ml.size());
		for(int i=0;i<ml.size(); i++) System.out.print(ml.get(i)+" ");
		ml.clear();
		System.out.println("\n"+ml.size());
		System.out.println(ml);

	}

}
